/* 
Set directory 
*/

    global root ENTER DIRECTORY
	
	global aser 	$root\Analysis\Dta\ASER

	global crops	$root\Analysis\Dta\ICRISAT\
	
	global output 	ENTER DIRECTORY
	
use "$aser\aser_all", clear

keep if math != . & read != .

merge m:1 gisstatename gisdistrictname  using "$crops\aser_dist_crops_geomatch.dta"

drop if _merge == 1

drop _merge

merge m:1 gisstatename gisdistrictname year using "$data\clean_weather_bins_hum_wbgt.dta"

**Dropping those not matcher from using
drop if _merge == 2


*State ID
*Andhra Pradesh/Telengana
replace gisstatename = "Andhra Pradesh" if gisstatename == "Telangana"


egen gstateid = group(gisstatename)

*Creating New District Id
drop gdistid

egen gdistid = group(gisstatename gisdistrictname)


**On-track children
replace age5to16class = . if age5to16class == 99
gen age_m_grade = childage - age5to16class

gen ontrack = .
replace ontrack = 1 if age_m_grade >= 0 &  age_m_grade <= 6
replace ontrack = 0 if ontrack != 1


drop if gisstatename == "Andaman and Nicobar"



tab ll1lrain, gen(ll1lrain)
tab ll1krain, gen(ll1krain)
tab ll1nkrain, gen(ll1nkrain)

label var nmath "Math Score (in SD)"
label var nread "Read Score (in SD)"

label var ll1totlbinlo "PY Days $<$15C"
label var ll1totlbinup "PY Days $>$21C"

label var ll1lbin1 "PY Days $<$13C"
label var ll1lbin2 "PY Days 13-15C"
label var ll1lbin4 "PY Days 17-19C"
label var ll1lbin5 "PY Days 19-21C"
label var ll1lbin6 "PY Days 21-23C"
label var ll1lbin7 "PY Days 23-25C"
label var ll1lbin8 "PY Days 25-27C"
label var ll1lbin9 "PY Days 27-29C"
label var ll1lbin10 "PY Days $>$29C"

gen heat_tot_crops_up1 = ll1totlbinup*heat_tot_crops

gen heat_tot_crops_29 = ll1lbin10*heat_tot_crops

label var heat_tot_crops_up1 "PY Days $>$21C * HRC"

label var heat_tot_crops_29 "PY Days $>$29C * HRC"


*Table 4
local tablename "T4"
local table=1
local col=1
estimates clear	
		   	   
areg nmath ll1totlbinlo ll1totlbinup heat_tot_crops_up1 ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'

areg nread ll1totlbinlo ll1totlbinup heat_tot_crops_up1 ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'


esttab cT`table'C* using "$output/`tablename'.tex", replace f ///
               label booktabs b(3) p(3) eqlabels(none) alignment(S) collabels("\multicolumn{1}{c}{$\beta$ / SE}") ///
               keep (ll1totlbinlo ll1totlbinup heat_tot_crops_up1, relax) ///
			   star(* 0.10 ** 0.05 *** 0.01) ///
               cells("b(fmt(4)star)" "se(fmt(4)par)") ///
			   stats(N r2, fmt(0 3) layout("\multicolumn{1}{c}{@}") ///
			   labels(`"Observations"' `"\(R^{2}\)"'))		
			   

xtile decile = avg_district_temp, nq(10)
label var decile "Climate Deciles"

bys decile: egen dec_avg_district_temp = max(avg_district_temp)

bys decile: egen heat_res = mean(C4_frac_ave)

label var heat_res "Fraction Area Grown with Heat-Resistant Crops"
label var dec_avg_district_temp "Avg. Temp. (C)"

bys decile: keep if _n == 1

*Figure 5 (Panel A)
twoway (connected heat_res decile, sort msymbol(O) yaxis(1) ylabel(0(0.05)0.35, axis(1)) lcolor(red) mcolor(red))  ///
(bar dec_avg_district_temp decile, sort  yaxis(2) ylabel(16(2)28, axis(2)) lcolor(gs14) color(none) ), graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (2 1)  label(2 "Average Temperature (C)") label(1 "Mean") ) ///
xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10")    
graph export "$output/F5a.png", replace width(800) height(600)		
